package com.example.javalab.convert;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ObjectConverter {
    public static void main(String[] args) {
        Object x = "abc";
        Command command=new Command();

//        String x2 = get(String.class, x);
//        System.out.println(x2);

//        Object o = command.execute("get", "name");
//        System.out.println("get(List.class,o) = " + get(List.class, o).getClass());

        System.out.println("get(\"String\") = " + get("String"));
        System.out.println("get(\"list\") = " + get("list"));
    }

    public static <T> T get(Class<T> clz, Object o) {
        System.out.println("(o instanceof List) = " + (o instanceof List));
        if (clz.isInstance(o)) {
            System.out.println("clz.isArray() = " + clz.isArray());
//            Type type = ((ParameterizedType)clz.getGenericSuperclass()).getActualTypeArguments()[0];
            ParameterizedType type = (ParameterizedType)clz.getGenericSuperclass();
            System.out.println("type==" + type);
            if(clz.isArray()){
                Class<?> componentType = clz.getComponentType();
                System.out.println("componentType = " + componentType);
            }
            return clz.cast(o);
        }
        return null;
    }

    public static <T> T get(String type) {
        if(type.equals("String")){
            return (T) new String("this is string");
        }else{
            List<String> list=new ArrayList<String>();
            list.add("that");
            list.add("is");
            list.add("list");
            return (T) list;
        }

    }
    static class Command {
        public Object execute(Object... args) {
            return Arrays.asList("get", "name");
        }
    }


}
